//================================================================================
//read file,get string
//================================================================================

import java.io.*;
import java.util.*;

public class StringProcess{
	private String version="2013/08/10";

	private int newstringcount;
	private String[] newstring;
	private int DBG=0;


	public StringProcess(){
		newstring=new String[1];
		newstringcount=0;
		DBG=0;
	}

	//================================================================================
	//================================================================================
	public String getVersion(){
		return version;
	}
	//================================================================================
	//DBG & 0x02
	//================================================================================
	/**
	parse file to one line string
	*/

	public String[] parseString(String orgString,String seperateString,String[] removeString){


//System.out.println("0---------------------------");
//System.out.println("oldstringlen:"+orgString.length());
//for(int n=0;n<orgString.length();n++){
//	System.out.print(orgString.charAt(n));
//}
//System.out.println();
//System.out.println("----------------------------");
//System.out.println("seperateString:"+seperateString);
//System.out.println("----------------------------");
//for(int i=0;i<removeString.length;i++){
//	System.out.println("removeString:"+removeString[i]);
//}
//System.out.println("----------------------------");

		newstringcount=0;

		String[] tmpString=orgString.split(seperateString);
		String[] tmpString_1=new String[tmpString.length];

		if(tmpString.length>0){
			

//System.out.println("1---------------------------");
//System.out.println("tmpString.length:"+tmpString.length);
//System.out.println("removeString.length:"+removeString.length);

//System.out.println("oldstring_1len:"+oldstring_1.length);
//System.out.println("newstring_1len:"+newstring_1.length);
//System.out.println("char:");
//for(int p=0;p<tmpString.length;p++){
//for(int n=0;n<tmpString[p].length();n++){
//System.out.print((int)tmpString[p].charAt(n));
//}
//System.out.println();
//}
//System.out.println();


if((DBG & 0x02)==0x02){
System.out.println("-- DBG parseString ----------------------");
}
			for(int i=0;i<tmpString.length;i++){

				for(int j=0;j<removeString.length;j++){
					while(tmpString[i].indexOf(removeString[j])!=-1){
						tmpString[i]=tmpString[i].replaceFirst(removeString[j],"");
					}
				}
				
				if(!tmpString[i].equals("")){//if not empty string save tmp save
					tmpString_1[newstringcount]=tmpString[i];
					newstringcount++;
				}

//System.out.println("2---------------------------:"+newstringcount);
//System.out.print("tmpString["+i+"]="+tmpString[i]);
//System.out.println("--");

			}
			
			newstring=new String[newstringcount];

//System.out.println("3---------------------------:"+newstringcount);

			for(int i=0;i<newstringcount;i++){
				newstring[i]=tmpString_1[i];
if((DBG & 0x02)==0x02){
System.out.println("newstring["+i+"]="+newstring[i]);
}
			}
if((DBG & 0x02)==0x02){
System.out.println("newstring.length="+newstring.length);
}
		}

		return newstring;
	}

	//================================================================================
	//DBG & 0x01
	//================================================================================
	/**
	check each string's function for next use 
	*/
/*
	public int[] parseFunction(String[] orgString,String[] funString){
		int k=1;


		if(orgString.length>0 && funString.length>0){
if((DBG & 0x01)==0x01){

System.out.println("-- DBG parseFunction --------------------");

}
			for(int i=0;i<orgString.length;i++){
//if((DBG & 0x01)==0x01)
//System.out.println("orgString["+i+"]="+orgString[i]);
				k=1;
	
				orgString[i]=orgString[i].toLowerCase();

				for(int j=0;j<funString.length;j++){
//if((DBG & 0x01)==0x01)
//System.out.println("funString["+j+"]="+funString[j]);
					if(orgString[i]!=null){
						
						funString[j]=funString[j].toLowerCase();

						if(orgString[i].indexOf(funString[j])!=-1){
							function[i]|=k;
						}
					}

					if(funString.length>=32)
						k=0;
					else
						k<<=1;
				}
if((DBG & 0x01)==0x01)
System.out.println("function["+i+"]="+function[i]);
			}
		}
		else{
			System.out.println("error:array length=0");	
		}

		return function;
	}
*/
	//================================================================================
	//================================================================================
	public String getPartOfString(String inputString,String TargetString,String seperateString){
		String tmpSting;

	

		if(inputString.equals(null) || TargetString.equals(null) || seperateString.equals(null)){
			return null;
		}


//		inputString=inputString.toLowerCase();
//		TargetString=TargetString.toLowerCase();
//		seperateString=seperateString.toLowerCase();

		tmpSting=inputString.replace(seperateString,"");

//System.out.println("tmpSting:"+tmpSting);
//System.out.println("0:"+inputString.indexOf(TargetString)+"-"+inputString.length());
//System.out.println("TargetString:"+TargetString);
		if(tmpSting.indexOf(TargetString)==-1){//not exist
//System.out.println("1");
			return null;
		}
		else if((tmpSting.indexOf(TargetString)+TargetString.length())==tmpSting.length()){
//System.out.println("2");
			return "";
		}
		else{
//System.out.println("3-0:"+(inputString.indexOf(TargetString)+TargetString.length()));
//System.out.println("3-1:"+inputString.length());
//System.out.println("3");
			inputString=inputString.substring(inputString.indexOf(TargetString),inputString.length());

//System.out.print(":"+inputString);
//System.out.println();
			if(inputString.indexOf(seperateString)==-1){
				return inputString.substring(TargetString.length(),inputString.length());
			}
			else{
				return inputString.substring(TargetString.length(),inputString.indexOf(seperateString));
			}
		}

	}
	//================================================================================
	//================================================================================
	private byte getHalfHex(byte inputbyte){
		byte[] bytearray={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};

//System.out.println("inputbyte="+inputbyte);
		if(inputbyte>=0x30 && inputbyte<=0x39){//0~9
//System.out.println("0");
			return (byte)(inputbyte-0x30);
		}
		else if(inputbyte>=0x41 && inputbyte<=0x46){//A~F
//System.out.println("1");
			return (byte)bytearray[inputbyte-0x41+10];
		}
		else if(inputbyte>=0x61 && inputbyte<=0x66){//a~f
//System.out.println("2");
			return (byte)bytearray[inputbyte-0x61+10];
		}
		else{
//System.out.println("3");
			return 0x00;
		}
	}

	//================================================================================
	//================================================================================

	public byte[] stringToByte(String orgString){
		byte[] newbyte;
		byte[] tmpbyte=new byte[0];

		if(orgString==null || orgString=="")//return len=0 array
			return tmpbyte;

		orgString.toUpperCase();
		

		if(orgString.length()%2==0){	//even
			newbyte=new byte[(orgString.length()/2)];
			tmpbyte=orgString.getBytes();
			for(int i=0;i<newbyte.length;i++){
				newbyte[i]|=getHalfHex(tmpbyte[i*2]);
				newbyte[i]<<=4;
				newbyte[i]|=getHalfHex(tmpbyte[i*2+1]);
//System.out.println("0:newbyte["+i+"]:"+newbyte[i]);

			}
		}
		else{				//odd
			orgString=orgString+"0";
			newbyte=new byte[orgString.length()/2];
			tmpbyte=orgString.getBytes();
			for(int i=0;i<newbyte.length;i++){
				newbyte[i]|=getHalfHex(tmpbyte[i*2]);
				newbyte[i]<<=4;
				newbyte[i]|=getHalfHex(tmpbyte[i*2+1]);
//System.out.println("1:newbyte["+i+"]:"+newbyte[i]);

			}
		}

		return newbyte;
	}
	//================================================================================
	//================================================================================
	private String[] bytestring={	"0","1","2","3","4","5","6","7",
					"8","9","A","B","C","D","E","F"};
	public String byteToString(byte[] inputbyte){
		String back="";
//System.out.println("inputbyte[0]:"+inputbyte[0]);
		if(inputbyte.length>0){

			for(int i=0;i<inputbyte.length;i++){
//System.out.println("inputbyte["+i+"]:"+inputbyte[i]);
				back+=bytestring[(inputbyte[i]&0xF0)>>4];
				back+=bytestring[inputbyte[i]&0x0F];
			}
		}
		return back;
	}

	//================================================================================
	//================================================================================
	public int getDBG(){
		return DBG;
	}
	//================================================================================
	//================================================================================
	public void setDBG(int dbg){
		DBG=dbg;
	}

	//================================================================================
	//================================================================================
/*
	public int[] getFunction(){
		return function;
	}
*/
	//================================================================================
	//================================================================================


}


